<?php

namespace App\Http\Controllers\Backstage\Resources;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Request;


//资源模块
class ResourcesCateController extends Controller
{
    //获取资源分类

    public function getResourcesCate(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('resources_cate')
            ->leftjoin('upload_file', 'resources_cate.fileid', '=', 'upload_file.fileid')
            ->orderBy('resources_catid', 'desc')
            ->get();
        if ($info) {
            $info = json_decode($info, true);
            foreach ($info as $key => $value) {
                $info[$key]['resources_cate_time'] = date('Y-m-d', $value['resources_cate_time']);
                $info[$key]['url'] =  http_type() . $_SERVER['HTTP_HOST'] . "/" . $info[$key]['url'] . "/" . $info[$key]['file_name'];
                foreach ($info as $k => $v) {
                    if ($value['resources_catid'] == $v['parentid']) {
                        $info[$key]['children'][$k] = $v;
                        $info[$key]['children'] = array_values($info[$key]['children']);
                    }
                }
            }
            foreach ($info as $keys => $values) {
                if ($values['parentid'] != '0') {
                    unset($info[$keys]);
                }
            }
            $info = array_values($info);
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }



    //获取全部分类

    public function getAllResourcesCate(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $info = DB::table('resources_cate')
            ->orderBy('resources_catid', 'desc')->get();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }



    //获取资源父级分类

    public function getParentResourcesCate(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['parentid'] = 0;
        $info = DB::table('resources_cate')
            ->where($map)
            ->orderBy('resources_catid', 'desc')->get();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }

    //新增资源分类

    public function addResourcescate(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['parentid'] = (int) $request->input('parentid');
        $date['fileid'] = (int) $request->input('fileid');
        $date['resources_cate_title'] = addslashes($request->input('resources_cate_title'));
        $date['resources_cate_introduction'] = addslashes($request->input('resources_cate_introduction'));
        $date['resources_cate_time'] = time();
        $info = DB::table('resources_cate')->insert($date);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //修改资源分类
    public function editResourcescate(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['resources_catid'] = $request->input('resources_catid');
        $date['fileid'] = (int) $request->input('fileid');
        $date['resources_cate_title'] = $request->input('resources_cate_title');
        $date['resources_cate_introduction'] = addslashes($request->input('resources_cate_introduction'));
        $info = DB::table('resources_cate')->where($map)->update($date);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //删除资源分类
    public function delResourcescate(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['resources_catid'] = $request->input('resources_catid');
        $info = DB::table('resources_cate')->where($map)->delete();
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
